#include "L298N.h"
#include "main.h"

// 引脚定义 - TIM通道引脚
#define MOTOR_A_IN1_PIN GPIO_PIN_0  // TIM2_CH1 - PA0
#define MOTOR_A_IN1_PORT GPIOA
#define MOTOR_A_IN2_PIN GPIO_PIN_1  // TIM2_CH2 - PA1  
#define MOTOR_A_IN2_PORT GPIOA
#define MOTOR_B_IN3_PIN GPIO_PIN_2  // TIM2_CH3 - PA2
#define MOTOR_B_IN3_PORT GPIOA
#define MOTOR_B_IN4_PIN GPIO_PIN_3  // TIM2_CH4 - PA3
#define MOTOR_B_IN4_PORT GPIOA

// 定时器句柄
static TIM_HandleTypeDef *pwm_tim;

// 电机状态 - 只保留方向变量，删除速度变量
static Motor_Direction motorA_dir = MOTOR_STOP;
static Motor_Direction motorB_dir = MOTOR_STOP;

void L298N_Init(TIM_HandleTypeDef *htim)
{
    // 保存定时器句柄
    pwm_tim = htim;
    
    // 启动所有PWM通道
    HAL_TIM_PWM_Start(pwm_tim, TIM_CHANNEL_1);  // PA0
    HAL_TIM_PWM_Start(pwm_tim, TIM_CHANNEL_2);  // PA1  
    HAL_TIM_PWM_Start(pwm_tim, TIM_CHANNEL_3);  // PA2
    HAL_TIM_PWM_Start(pwm_tim, TIM_CHANNEL_4);  // PA3
    
    // 初始停止所有电机
    L298N_StopAll();
}

void L298N_MotorControl(Motor_Select motor, Motor_Direction dir, uint8_t speed)
{
    // 限制速度范围 0-100
    if(speed > 100) speed = 100;
    
    // 转换为PWM值 (假设ARR=999, 0-1000对应0%-100%)
    uint16_t pwm_value = speed * 10;
    
    switch(motor) {
        case MOTOR_A:
            motorA_dir = dir;
            // 删除了 motorA_speed = speed;
            
            switch(dir) {
                case MOTOR_STOP:
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_1, 0);  // IN1 PWM=0
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_2, 0);  // IN2 PWM=0
                    break;
                    
                case MOTOR_FORWARD:
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_1, pwm_value);  // IN1 PWM
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_2, 0);          // IN2=0
                    break;
                    
                case MOTOR_BACKWARD:
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_1, 0);          // IN1=0
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_2, pwm_value);  // IN2 PWM
                    break;
                    
                case MOTOR_BRAKE:
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_1, 1000);  // IN1=100%
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_2, 1000);  // IN2=100%
                    break;
            }
            break;
            
        case MOTOR_B:
            motorB_dir = dir;
            // 删除了 motorB_speed = speed;
            
            switch(dir) {
                case MOTOR_STOP:
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_3, 0);  // IN3 PWM=0
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_4, 0);  // IN4 PWM=0
                    break;
                    
                case MOTOR_FORWARD:
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_3, pwm_value);  // IN3 PWM
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_4, 0);          // IN4=0
                    break;
                    
                case MOTOR_BACKWARD:
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_3, 0);          // IN3=0
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_4, pwm_value);  // IN4 PWM
                    break;
                    
                case MOTOR_BRAKE:
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_3, 1000);  // IN3=100%
                    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_4, 1000);  // IN4=100%
                    break;
            }
            break;
    }
}

void L298N_StopAll(void)
{
    motorA_dir = MOTOR_STOP;
    motorB_dir = MOTOR_STOP;
    // 删除了 motorA_speed = 0; 和 motorB_speed = 0;
    
    // 所有PWM输出为0
    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_1, 0);
    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_2, 0);
    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_3, 0);
    __HAL_TIM_SET_COMPARE(pwm_tim, TIM_CHANNEL_4, 0);
}


